package com.ltw.playit.online.vedio.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ltw.playit.common.utils.JsonConverter;
import com.ltw.playit.online.vedio.bean.VedioSummaryBean;

@Service
public class VedioService {

	@Autowired
	JsonConverter jsonConverter;

	public String retrieveVedioSummary() throws JsonProcessingException {

		Map<String,Object> response = new HashMap<String,Object>();
		try {

			Document doc = Jsoup.connect("https://www.360kan.com//").get();
			Element topElement = doc.getElementById("tj-topslider");
			Elements dianshiElements = doc.getElementsByClass("p-mod dianshi");
			Elements zongYiElements = doc.getElementsByClass("content zycontent");
			Elements dyElements = doc.getElementsByClass("p-mod remendy");
			Elements rmDyElements = dyElements.get(0).getElementsByClass("content rmcontent");
			Elements dongManElements = doc.getElementsByClass("dongman g-clear").get(0)
					.getElementsByClass("content content-list");

			response.put("hot",ParseHotItems(topElement));
			response.put("dianshi",ParseDianShiItems(dianshiElements.get(0)));
			response.put("dingying",ParseDyItems(rmDyElements.get(0)));
			response.put("zongyi",ParseZongYiItems(zongYiElements.get(0)));
			response.put("dongman",ParseDongManItems(dongManElements.get(0)));

		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonConverter.asString(response);

	}

	private List<VedioSummaryBean> ParseDongManItems(Element element) {
		Map<String, List<VedioSummaryBean>> dongmanResultMap = new HashMap<String, List<VedioSummaryBean>>();
		List<VedioSummaryBean> dongmanList = new ArrayList<VedioSummaryBean>();
		Elements liElements = element.getElementsByTag("li");
		VedioSummaryBean vsb;
		for (Iterator<Element> iterator = liElements.iterator(); iterator.hasNext();) {
			Element liElement = (Element) iterator.next();
			if (liElement.hasClass("w-newfigure")) {
				String title = liElement.attr("title");
				String href = liElement.getElementsByTag("a").attr("href");
				Elements hitSpan = liElement.getElementsByClass("w-newfigure-hint");
				String hit = hitSpan.get(0).text();
				Elements detailDiv = liElement.getElementsByClass("w-newfigure-detail");
				String desc = detailDiv.get(0).getElementsByClass("w-newfigure-desc").text();
				String imgSrc = liElement.getElementsByTag("img").attr("data-src");
				vsb = new VedioSummaryBean();
				vsb.setTitle(title);
				vsb.setDetailHref(href);
				vsb.setDesc(desc);
				vsb.setHit(hit);
				vsb.setType("ct");
				vsb.setImgSrc(imgSrc);
				dongmanList.add(vsb);
			}
		}

		return dongmanList;
	}

	private List<VedioSummaryBean> ParseDyItems(Element element) {
		Map<String, List<VedioSummaryBean>> dianyingResultMap = new HashMap<String, List<VedioSummaryBean>>();
		List<VedioSummaryBean> dianyingList = new ArrayList<VedioSummaryBean>();

		Elements liElements = element.getElementsByTag("li");
		VedioSummaryBean vsb;
		for (Iterator<Element> iterator = liElements.iterator(); iterator.hasNext();) {
			Element liElement = (Element) iterator.next();
			if (liElement.hasClass("w-newfigure")) {
				String title = liElement.attr("title");
				String href = liElement.getElementsByTag("a").attr("href");
				Elements hitSpan = liElement.getElementsByClass("w-newfigure-hint");
				String hit = hitSpan.get(0).text();
				Elements detailDiv = liElement.getElementsByClass("w-newfigure-detail");
				String desc = detailDiv.get(0).getElementsByClass("w-newfigure-desc").text();
				String imgSrc = liElement.getElementsByTag("img").attr("data-src");
				vsb = new VedioSummaryBean();
				vsb.setTitle(title);
				vsb.setDetailHref(href);
				vsb.setDesc(desc);
				vsb.setHit(hit);
				vsb.setType("m");
				vsb.setImgSrc(imgSrc);
				dianyingList.add(vsb);
			}
		}

		return dianyingList;
	}

	private List<VedioSummaryBean> ParseZongYiItems(Element element) {
		Map<String, List<VedioSummaryBean>> zongyiResultMap = new HashMap<String, List<VedioSummaryBean>>();
		List<VedioSummaryBean> zongyiList = new ArrayList<VedioSummaryBean>();

		Elements liElements = element.getElementsByTag("li");
		VedioSummaryBean vsb;
		for (Iterator<Element> iterator = liElements.iterator(); iterator.hasNext();) {
			Element liElement = (Element) iterator.next();
			if (liElement.hasClass("w-newfigure")) {
				String title = liElement.attr("title");
				String href = liElement.getElementsByTag("a").attr("href");
				Elements hitSpan = liElement.getElementsByClass("w-newfigure-hint");
				String hit = hitSpan.get(0).text();
				Elements detailDiv = liElement.getElementsByClass("w-newfigure-detail");
				String desc = detailDiv.get(0).getElementsByClass("w-newfigure-desc").text();
				String imgSrc = liElement.getElementsByTag("img").attr("data-src");
				vsb = new VedioSummaryBean();
				vsb.setTitle(title);
				vsb.setDetailHref(href);
				vsb.setDesc(desc);
				vsb.setHit(hit);
				vsb.setType("va");
				vsb.setImgSrc(imgSrc);

				zongyiList.add(vsb);
			}
		}

		return zongyiList;
	}

	private List<VedioSummaryBean> ParseDianShiItems(Element element) {

		List<VedioSummaryBean> dianshiList = new ArrayList<VedioSummaryBean>();

		Elements liElements = element.getElementsByTag("li");
		VedioSummaryBean vsb;
		for (Iterator<Element> iterator = liElements.iterator(); iterator.hasNext();) {
			Element liElement = (Element) iterator.next();
			if (liElement.hasClass("w-newfigure")) {
				String title = liElement.attr("title");
				String href = liElement.getElementsByTag("a").attr("href");
				Elements hitSpan = liElement.getElementsByClass("w-newfigure-hint");
				String hit = hitSpan.get(0).text();
				Elements detailDiv = liElement.getElementsByClass("w-newfigure-detail");
				String desc = detailDiv.get(0).getElementsByClass("w-newfigure-desc").text();
				String imgSrc = liElement.getElementsByTag("img").attr("data-src");

				vsb = new VedioSummaryBean();
				vsb.setTitle(title);
				vsb.setDetailHref(href);
				vsb.setDesc(desc);
				vsb.setHit(hit);
				vsb.setType("tv");
				vsb.setImgSrc(imgSrc);

				dianshiList.add(vsb);
			}
		}

		return dianshiList;
	}

	private List<VedioSummaryBean> ParseHotItems(Element hotElement) {
		List<VedioSummaryBean> hotList = new ArrayList<VedioSummaryBean>();

		Elements topItems = hotElement.getElementsByTag("a");
		VedioSummaryBean vsb;
		for (Iterator<Element> iterator = topItems.iterator(); iterator.hasNext();) {
			Element element = (Element) iterator.next();
			String href = element.attr("href");
			
			if (href.indexOf("360kan") == -1) {
				continue;
			}
			
			Elements imgElements = element.getElementsByTag("img");
			String imgSrc = imgElements.attr("src");
			Elements titleSpan = element.getElementsByClass("title");
			Elements descSpan = element.getElementsByClass("desc");

			vsb = new VedioSummaryBean();
			vsb.setTitle(titleSpan.text());
			vsb.setDetailHref(href);
			vsb.setDesc(descSpan.text());
			vsb.setImgSrc(imgSrc);

			hotList.add(vsb);
		}

		return hotList;
	}

}
